﻿// -----------------------------------------------------------------------
// <copyright file="FileStore.cs" company="ANGIE.D">
//     Copyright (c) 2012 ANGIE.D. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MyFileStore.FileStore
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Serialization;

    public class FileStore
    {
        public string Name { get; set; }

        public string DirectoryPath { get; set; }

        public FSDirectoryInformation OfflineInformation { get; set; }

        public DateTime OfflineInformationDatetime { get; set; }

        public static TreeNode CreateTreeViewNode(FSDirectoryInformation fsdInfo)
        {
            TreeNode node = new TreeNode(fsdInfo.Name);

            foreach (var dir in fsdInfo.Directories)
            {
                node.Nodes.Add(CreateTreeViewNode(dir));
            }

            foreach (var fsfinfo in fsdInfo.Files)
            {
                TreeNode subNode = new TreeNode(
                    string.Format("{0} [{1}]",
                    fsfinfo.Name,
                    ToReadableSize(fsfinfo.Size))
                    );

                node.Nodes.Add(subNode);
            }

            return node;
        }

        public bool TryToRefreshOfflineInformation()
        {
            if (Directory.Exists(this.DirectoryPath))
            {
                this.OfflineInformation = this.GetFSDirectoryInformation(this.DirectoryPath);
                this.OfflineInformationDatetime = DateTime.Now;

                return true;
            }

            return false;
        }

        private static string ToReadableSize(long size)
        {
            double temp = size;

            if (temp < 1024)
            {
                return string.Format("{0} B", temp);
            }

            temp = temp / 1024;

            if (temp < 1024)
            {
                return string.Format("{0} KB", temp.ToString("n2"));
            }

            temp = temp / 1024;

            if (temp < 1024)
            {
                return string.Format("{0} MB", temp.ToString("n2"));
            }

            temp = temp / 1024;

            return string.Format("{0} GB", temp.ToString("n2"));
        }

        private FSDirectoryInformation GetFSDirectoryInformation(string directoryPath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);

            FSDirectoryInformation fsdInfo = new FSDirectoryInformation();
            fsdInfo.Name = directoryInfo.Name;
            fsdInfo.FullPath = directoryInfo.FullName;
            fsdInfo.CreationTime = directoryInfo.CreationTime;
            fsdInfo.LastWriteTime = directoryInfo.LastWriteTime;
            fsdInfo.LastAccessTime = directoryInfo.LastAccessTime;

            DirectoryInfo[] subDirectoryInfos;
            try
            {
                subDirectoryInfos = directoryInfo.GetDirectories();
            }
            catch
            {
                return null;
            }

            foreach (var dinfo in subDirectoryInfos)
            {
                FSDirectoryInformation subFSDInfo = this.GetFSDirectoryInformation(dinfo.FullName);

                if (subFSDInfo != null)
                {
                    fsdInfo.Directories.Add(subFSDInfo);
                }
            }

            FileInfo[] fileInfos;
            try
            {
                fileInfos = directoryInfo.GetFiles();
            }
            catch
            {
                return null;
            }

            foreach (var fInfo in fileInfos)
            {
                FSFileInformation subFSFInfo = new FSFileInformation();
                subFSFInfo.Name = fInfo.Name;
                subFSFInfo.FullPath = fInfo.FullName;
                subFSFInfo.Size = fInfo.Length;
                subFSFInfo.CreationTime = fInfo.CreationTime;
                subFSFInfo.LastWriteTime = fInfo.LastWriteTime;
                subFSFInfo.LastAccessTime = fInfo.LastAccessTime;

                fsdInfo.Files.Add(subFSFInfo);
            }

            return fsdInfo;
        }

        public void Save()
        {
            if (!Directory.Exists(Config.FSXMLFolder))
            {
                Directory.CreateDirectory(Config.FSXMLFolder);
            }

            string filePath = Path.Combine(Config.FSXMLFolder, this.Name + Config.FSXMLFileSuffix);

            Helper.Serializer<FileStore> fileStoreSerializer = new Helper.Serializer<FileStore>();
            fileStoreSerializer.Serialize(this, filePath);
        }

        public static FileStore Load(string fsName)
        {
            string filePath = Path.Combine(Config.FSXMLFolder, fsName + Config.FSXMLFileSuffix);

            if (File.Exists(filePath))
            {
                Helper.Serializer<FileStore> fileStoreSerializer = new Helper.Serializer<FileStore>();

                if (fileStoreSerializer.CanDeserialize(filePath))
                {
                    return fileStoreSerializer.Deserialize(filePath);
                }
            }

            return null;
        }
    }
}
